/**
 * 
 */

package com.yurivolkov.android.locale_audio_update_notifier;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

/**
 * The service is used to send delayed broadcast. This ensures that during delay
 * Android won't kill the process
 * 
 * @author yvolk (Yuri Volkov), http://yurivolkov.com
 */
public class TheService extends Service {
    // log tag
    private static final String TAG = TheService.class.getSimpleName();

    /*
     * (non-Javadoc)
     * @see android.app.Service#onBind(android.content.Intent)
     */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d(TAG, "onStart");
        super.onStart(intent, startId);

        // Delay in seconds
        int delay = Constants.DELAY_DEFAULT;

        // Optional toast (for debugging purposes and for curious people)
        String message0 = "";

        /**
         * v.1.06 intent may be null in a case of service restart...
         */
        if (intent != null) {
            delay = intent.getIntExtra(Constants.INTENT_EXTRA_DELAY, Constants.DELAY_DEFAULT);
            message0 = intent.getStringExtra(Constants.INTENT_EXTRA_MESSAGE);
        }
        final String message = message0;

        // Asynchronously send broadcast
        Runnable r = new Runnable() {
            public void run() {
                if (!TextUtils.isEmpty(message)) {
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
                    Log.d(TAG, "Toast: \"" + message + "\"");
                }

                // And this is the reason of the plug-in existence:
                Intent notificationIntent = new Intent(Constants.ACTION_AUDIO_VOLUME_UPDATE);
                notificationIntent.putExtra(Constants.EXTRA_STREAM_TYPE,
                        Constants.EXTRA_VALUE_UNKNOWN);
                notificationIntent.putExtra(Constants.EXTRA_VOLUME_INDEX,
                        Constants.EXTRA_VALUE_UNKNOWN);
                notificationIntent.putExtra(Constants.EXTRA_RINGER_MODE,
                        Constants.EXTRA_VALUE_UNKNOWN);

                Log.d(TAG, "On send Broadcast");
                getApplicationContext().sendBroadcast(notificationIntent, null);
                // No difference
                // cw.sendOrderedBroadcast(notificationIntent, null);

                TheService.this.stopSelf();
            }
        };

        /**
         * There was a report that delay=0 may cause an error, see
         * 
         * @see <a
         *      href="http://code.google.com/p/locale-audio-update-notifier/issues/detail?id=2">Toast.makeText
         *      causes FATAL EXCEPTION if delay=0</a>
         */

        // if (delay <= 0) {
        // Thread t = new Thread(r);
        // t.start();
        // } else {
        Handler mHandler = new Handler();
        Log.d(TAG, "Start Runnable, delay=" + delay);
        mHandler.postDelayed(r, delay * 1000 + 1);
        // }

    }

}
